<?php
ob_start();
session_start();
include "checksession.php";
date_default_timezone_set('Asia/Bangkok');
include("../connect.php");
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$strSQL = "SELECT * FROM employee INNER JOIN mail ON employee.emp_id = mail.emp_id where employee.emp_id = '".$_GET["emp_id"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$id_department = $objResult["de_id"];
if(!$objResult)
{
	echo "Not found Employee=".$_GET["emp_id"];
}
else
{
?>
<html>
<head>
<title>Adinop Co., Ltd. was founded as an ingredients importer and local manufacturers’ distributor in 1986. We also function as an exporter of finished products</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../css/style.css"/>
<link type="text/css" href="../css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script src="../js/jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/jquery/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="../js/jquery/timepicker.js"></script>
</head>
 <script type="text/javascript">
            $(function() {
                $('#datemail_authorized').datepicker({
					dateFormat: "yy-mm-dd" ,
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
				
				 $('#datemail_requestUser').datepicker({
					dateFormat: "yy-mm-dd" ,
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
				
				 $('#datemail_staffIT').datepicker({
					dateFormat: "yy-mm-dd" ,
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
				
				 $('#datemail_authorizedCon').datepicker({
					dateFormat: "yy-mm-dd" ,
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
				
				 $('#datemail_supervisor').datepicker({
					dateFormat: "yy-mm-dd" ,
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
            });
			
			$(function() {
                $('#txtStartdate').datepicker({
                    duration: '',
                    showTime: true,
                    constrainInput: false
                });
            });
</script>
<script language="JavaScript">
	   var HttPRequest = false;

	   function doCallAjax() {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	
		  var url = 'check_record_id.php';
		  var pmeters = "tEmployee_id=" + encodeURI( document.getElementById("txtEmployee_id").value );

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				if(HttPRequest.readyState == 3)  // Loading Request
				{
					document.getElementById("mySpan").innerHTML = "..";
				}

				if(HttPRequest.readyState == 4) // Return Request
				{
					if(HttPRequest.responseText == 'Y')
					{
						window.location = 'AjaxPHPRegister3.php';
					}
					else
					{
						document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
					}
				}
				
			}

	   }
	</script>
<script language="JavaScript">
	   var HttPRequest = false;

	   function doCallAjax1() {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	
		  var url = 'check_record_name.php';
		  var pmeters = "tName_eng=" + encodeURI( document.getElementById("txtName_eng").value );

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				if(HttPRequest.readyState == 3)  // Loading Request
				{
					document.getElementById("mySpan1").innerHTML = "..";
				}

				if(HttPRequest.readyState == 4) // Return Request
				{
					if(HttPRequest.responseText == 'Y')
					{
						window.location = 'AjaxPHPRegister3.php';
					}
					else
					{
						document.getElementById("mySpan1").innerHTML = HttPRequest.responseText;
					}
				}
				
			}

	   }
	</script>
<script type="text/javascript">
function KeyCodeInt(objId)
{
if (event.keyCode >= 48 && event.keyCode<=57) //48-57(ตัวเลข) ,65-90(Eng ตัวพิมพ์ใหญ่ ) ,97-122(Eng ตัวพิมพ์เล็ก)
{
return true;
}
else
{
alert("กรอกได้เฉพาะตัวเลข 0-9 เท่านั้นครับ");
event.returnValue = false;
}
}

function KeyCodeEng(objId)
{
if (event.keyCode>=97 && event.keyCode<=122 ) //48-57(ตัวเลข) ,65-90(Eng ตัวพิมพ์ใหญ่ ) ,97-122(Eng ตัวพิมพ์เล็ก)
{
return true;
}
else
{
alert("กรอกได้เฉพาะตัวพิมพ์เล็ก a-z");
event.returnValue = false;
}
}

function CheckThai(sText,obj){
	var ValidChars = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬหอฮ ะา ิ ี ึ ื ุ ูฤๅฦ ์ ำ ๋ ๊ ็ ่ ้ ั โไใ เ แ '; ";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++){   
		Char = sText.charAt(i);   
		if (ValidChars.indexOf(Char) == -1){  
			IsNumber = false;
		}
	}
	if(IsNumber==false){
		alert("กรอกได้เฉพาะภาษาไทย");
		obj.value="";
	}
}
</script>
<script language="javascript">
			function CheckValidate()
			{
			if(eval("document.frmMain.txtName_eng.value")==""){
				alert("กรุณากรอกช่อง Name(ENG)");
				eval("document.frmMain.txtName_eng.focus();")
				return false;
				}

			if(eval("document.frmMain.txtSurname_eng.value")==""){
				alert("กรุณากรอกช่อง Surname(ENG)");
				eval("document.frmMain.txtSurname_eng.focus();")
				return false;
				}

			if(eval("document.frmMain.txtDepartment.value")==""){
				alert("กรุณาเลือกช่อง Department");
				eval("document.frmMain.txtDepartment.focus();")
				return false;
				}

			if(eval("document.frmMain.txtName_th.value")==""){
				alert("กรุณากรอกช่อง Name(TH)");
				eval("document.frmMain.txtName_th.focus();")
				return false;
				}

			if(eval("document.frmMain.txtSurname_th.value")==""){
				alert("กรุณากรอกช่อง Surname(TH)");
				eval("document.frmMain.txtSurname_th.focus();")
				return false;
				}

			if(eval("document.frmMain.txtUsername.value")==""){
				alert("กรุณากรอกช่อง Username");
				eval("document.frmMain.txtUsername.focus();")
				return false;
				}

			if(eval("document.frmMain.txtadmaillocal.value")==""){
				alert("กรุณากรอกช่อง admail.local");
				eval("document.frmMain.txtadmaillocal.focus();")
				return false;
				}

			if(eval("document.frmMain.txtadinopcoth.value")==""){
				alert("กรุณากรอกช่อง adinop.co.th");
				eval("document.frmMain.txtadinopcoth.focus();")
				return false;
				}

			if(document.frmMain.chkreq.checked == false){
				alert("กรุณาติ๊กช่อง I agree");
				document.frmMain.chkreq.focus();
				return false;
				}

			if(document.frmMain.txtName_eng.value != document.frmMain.txtUsername.value){
				alert("กรุณากรอกชื่อให้เหมือนกับ Username");
				document.frmMain.txtUsername.focus();
				return false;
				}

			}
</script>
<script type='text/javascript'>
function check_emaillocal(elm){
    var regex_email=/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*\@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{2,5})$/
    if(!elm.value.match(regex_email)){
		elm.value='';
        alert('รูปแบบ email ไม่ถูกต้อง example@admail.local');
    }
}
</script>

</script>
<body>
   <body class="body">

            <table align="center" width="90%">
                <tr>
                    <td colspan="2" class="header" >
                      <img src="../images/logo.png"> ใบทะเบียนประวัติเจ้าหน้าที่ 
                        <hr/>
                    </td>
                </tr >
              </table>
<br/>
<div align="center">
 <fieldset>
<legend class="nameTitle">ข้อมูลส่วนตัว</legend>
<form action="<? $editFormAction;?>" method="post" name ="frmMain" id="frmMain" OnSubmit="return CheckValidate();" />
<table width="100%" border="0" class="inputcontenttable">
  <tr>
    <th width="30%">
      <div align="right">Employee ID : </div></th>
    <th><div align="left">
      <input type="text" name="txtEmployee_id" id="txtEmployee_id" size="20" value="<?=$objResult["emp_id"];?>" maxlength= "8" placeholder="รหัสเจ้าหน้าที่" value="" OnChange="JavaScript:doCallAjax();" onkeypress="KeyCodeInt()" readonly="" />
	  <span id="mySpan"></span></div></th>
	  <th width="12%">
      <div align="right">Department : </div></th>
	   <td><select name="txtDepartment" id="txtDepartment">
			<option value=""><-- Please Select Item --></option>
			<?
			$strSQLdepart = "SELECT * FROM depart ORDER BY id ASC";
			$objQuerydepart = mysql_query($strSQLdepart);
			while($objResuutdepart = mysql_fetch_array($objQuerydepart))
			{
				if($id_department == $objResuutdepart["id"])
				{
					$sel = "selected";
				}
				else
				{
					$sel = "";
				}
			?>
			<option value="<?=$objResuutdepart["id"];?>" <?=$sel;?>><?=$objResuutdepart["departname"];?></option>
			<?
			}
			?>
		  </select>
		</td>
  </tr>

  <tr>
    <th width="25%">
      <div align="right">Name(ENG) : </div></th>
    <th><div align="left">
      <input type="text" name="txtName_eng" id="txtName_eng" size="20" value="<?=$objResult["emp_nameEng"];?>" placeholder="ชื่อภาษาอังกฤษ" OnChange="JavaScript:doCallAjax1();" onkeypress="KeyCodeEng()" />
	  <span id="mySpan1"></span></div></th>
    </div></th>

	<th width="12%">
      <div align="right">Name(TH) : </div></th>
	  <td><input type="text" name="txtName_th" id="txtName_th" size="20" value="<?=$objResult["emp_nameTh"];?>" placeholder="ชื่อภาษาไทย" onkeyup="CheckThai(this.value,this)" /></td>
  </tr>

  <tr>
    <th width="25%">
      <div align="right">Surname(ENG) : </div></th>
    <th><div align="left">
      <input type="text" name="txtSurname_eng" id="txtSurname_eng" value="<?=$objResult["emp_surnameEng"];?>" size="20" placeholder="นามสกุลภาษาอังกฤษ" onkeypress="KeyCodeEng()" />
    </div></th>

	<th width="12%">
      <div align="right">Surname(TH) : </div></th>
	  <td><input type="text" name="txtSurname_th" id="txtSurname_th" size="20" value="<?=$objResult["emp_surnameTh"];?>" placeholder="นามสกุลภาษาไทย" onkeyup="CheckThai(this.value,this)"/></td>
  </tr>

  <tr>
    <th width="25%"> 
	<div align="right">Start Date : </div></th>
    <th width="236"><div align="left">
      <input type="text" name="txtStartdate" id="txtStartdate" size="20" value="<?=$objResult["emp_startDate"];?>"/>
    </div></th>
    </tr>

	<tr>
    <th width="25%">
      <div align="right">Username : </div></th>
    <th><div align="left">
      <input type="text" name="txtUsername" id="txtUsername" size="20" value="<?=$objResult["mail_username"];?>" placeholder="ชื่อภาษาอังกฤษ" onkeypress="KeyCodeEng()"/>

	  <th width="12%">
      <div align="right">Remark : </div></th>
	  <td><textarea name="txtRemark" id="txtRemark" rows="number" cols="number" wrap="off" placeholder="หมายเหตุ"/><?=$objResult["mail_remark"];?></textarea></td>
  </tr>

  <tr>
    <th width="25%" align="right">
	  <div align="right">admail.local : </div></th>
    <th><div align="left">
	  <input type="text" name="txtadmaillocal" id="txtadmaillocal" size="20" value="<?=$objResult["mail_local"];?>" placeholder="อีเมล์ภายใน"  onblur='check_emaillocal(this)'/>
    </div></th>

	<th width="12%">
      <div align="right">adinop.co.th : </div></th>
	  <td><input type="text" name="txtadinopcoth" id="txtadinopcoth" size="20" value="<?=$objResult["mail_coth"];?>" placeholder="อีเมล์ภายนอก"  /></td>
  </tr>

   <tr>
    <th width="25%" align="center">
	  <div align="right">No. 13-S : </div></th>
    <th><div align="left">
	  <input type="text" name="txtno" id="txtno" size="20" placeholder=""  value="<?=$objResult["emp_no13u"];?>" onkeypress="KeyCodeInt()"/>
    </div></th>
  </tr>
</table>
<br/>
<input type="checkbox" name="chkreq" id="chkreq" value="Accept" />I agree
 </fieldset>
<br/>
<input name="btnRecordedit" type="submit" id="btnRecordedit" value="แก้ไข" style="height: 30px; width: 120px" />
</form>
</div>
<?
if($objResult["mail_authorizedDate"]=='0000-00-00 00:00:00'){
   $objResult["mail_authorizedDate"]=xxxx;
   }
if($objResult["mail_requestUserDate"]=='0000-00-00 00:00:00'){
   $objResult["mail_requestUserDate"]=xxxx;
   }
if($objResult["mail_staffITdate"]=='0000-00-00 00:00:00'){
   $objResult["mail_staffITdate"]=xxxx;
   }
if($objResult["mail_authorizedConDate"]=='0000-00-00 00:00:00'){
   $objResult["mail_authorizedConDate"]=xxxx;
   }
if($objResult["mail_supervisorDate"]=='0000-00-00 00:00:00'){
   $objResult["mail_supervisorDate"]=xxxx;
   }
?>

<div align="center">
<br/><br/><br/><br/><br/><br/>
 <fieldset>
<legend class="nameTitle">อัพเดจลายเซ็นและวันที่</legend>
<form action="updateDatetimeRecord.php" method="post" name ="frmMain2" id="frmMain2" />
<table width="100%" border="0" class="inputcontenttable">
 <tr>
    <th width="30%">
      <div align="right">mail_authorized : </div></th>
    <th><div align="left">
      <input type="text" name="datemail_authorized" id="datemail_authorized" size="20" value="<?=$objResult["mail_authorizedDate"];?>" />
	</div></th>
	  <th width="12%">
      <div align="right">mail_requestUser : </div></th>
	   <td><input type="text" name="datemail_requestUser" id="datemail_requestUser" size="20" value="<?=$objResult["mail_requestUserDate"];?>" /></td>
  </tr>

  <tr>
    <th width="25%">
      <div align="right">mail_staffIT : </div></th>
    <th><div align="left">
      <input type="text" name="datemail_staffIT" id="datemail_staffIT" size="20" value="<?=$objResult["mail_staffITdate"];?>" />
	  </th>
    </div></th>

	<th width="12%">
      <div align="right">mail_authorizedCon : </div></th>
	  <td><input type="text" name="datemail_authorizedCon" id="datemail_authorizedCon" size="20" value="<?=$objResult["mail_authorizedConDate"];?>" /></td>
  </tr>

  <tr>
    <th width="25%"> 
	<div align="right">mail_supervisor : </div></th>
    <th width="236"><div align="left">
      <input type="text" name="datemail_supervisor" id="datemail_supervisor" size="20" value="<?=$objResult["mail_supervisorDate"];?>" />
    </div></th>
    </tr>
 
</table>
<br/>
 </fieldset>
<br/>
<input type="hidden" name="mail_id" id="mail_id" size="20" value="<?=$objResult["mail_id"];?>" />
<input type="hidden" name="txtEmployee_id1" id="txtEmployee_id1" size="20" value="<?=$objResult["emp_id"];?>" />
<input name="btnRecordedit2" type="submit" id="btnRecordedit2" value="แก้ไข" style="height: 30px; width: 120px" />
</form>
</div>

</body>
  <? 
	  if(isset($_POST['btnRecordedit'])){
	  $txtEmployee_id = $_POST['txtEmployee_id'];
	  $txtName_eng = $_POST['txtName_eng'];
	  $txtSurname_eng = $_POST['txtSurname_eng'];	  
	  $txtStartdate = $_POST['txtStartdate'];
//	  $txtDepartname = $_POST['txtDepartname'];
	  $txtDepartment = $_POST['txtDepartment'];
	  $txtName_th = $_POST['txtName_th'];
	  $txtSurname_th = $_POST['txtSurname_th'];
	  $txtUsername = $_POST['txtUsername'];
	  $txtadmaillocal = $_POST['txtadmaillocal'];
	  $txtadinopcoth = $_POST['txtadinopcoth'];
	  $txtRemark = $_POST['txtRemark'];
	  $txtno = $_POST['txtno'];
	  $chkreq = $_POST['chkreq'];
//	  $txtdatetimereq = $_POST['txtdatetimereq'];
	  $sessionuserid = $_SESSION["UserID"];
		
	  $sql3 = "select * from employee where emp_no13u = '$txtno' and emp_id != '$txtEmployee_id' ";
	  $query_id3 = mysql_query($sql3);
	  if(mysql_num_rows($query_id3) > 0){
		echo "	<script language='javascript'>
				alert('รหัสNO. 13-S ซ้ำ');
				</script>";
				exit();
		}
		
//	  $sql = "SELECT * FROM `employee` where `emp_id` = '{$txtEmployee_id}' and `emp_nameEng` != '{$txtName_eng}'";
	  $sql = "SELECT * FROM `employee` where `emp_nameEng` = '{$txtName_eng}' and `emp_id` != '{$txtEmployee_id}'";
	  $query_id = mysql_query($sql);
	  if($rownum = mysql_num_rows($query_id) > 0){
		echo "	<script language='javascript'>
				alert('ชื่อภาษาอังกฤษซ้ำ');
				</script>";
				exit();
		}
		else{
?>
<?	  
		$sqldepart = "select departname from depart where id = '$txtDepartment' ";
		$query_departname = mysql_query($sqldepart);
		while ($objResult = mysql_fetch_array($query_departname)) {
				$txtDepartname = $objResult['departname'];
		}
?>
<?
	
		$sql1 = "update employee set emp_nameEng= '$txtName_eng' , emp_surnameEng= '$txtSurname_eng' , emp_nameTh='$txtName_th', emp_surnameTh='$txtSurname_th', emp_no13u='$txtno' , de_id='$txtDepartment' where emp_id ='$txtEmployee_id' ";
		$rsUpdate=mysql_query($sql1);

//		$sql1 = "UPDATE  `db_itmanager`.`employee` SET  `emp_nameEng` =  '$txtName_eng',
//				`emp_surnameEng` =  '$txtSurname_eng',
//				`emp_startDate` =  '$txtStartdate',
//				`emp_nameTh` =  '$txtName_th',
//				`emp_surnameTh` =  '$txtSurname_th',
//				`emp_no13u` =  '$txtno',
//				`de_id` =  '$txtDepartment' WHERE  `employee`.`emp_id` ='$txtEmployee_id';";
//		$rsUpdate=mysql_query($sql1);

		$sql2 = "update mail set mail_username='$txtUsername', mail_local='$txtadmaillocal', mail_coth='$txtadinopcoth', mail_remark='$txtRemark', mail_request='$chkreq', mail_requestDate=SYSDATE() where emp_id='$txtEmployee_id' ";
		$rsUpdate2=mysql_query($sql2);

	   if($rsUpdate2){
		echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
		 echo "<meta http-equiv='refresh' content='1; url=recordadd.php'>";
		}
	}
}
}

  mysql_close($connection);
  ?>

</html>